package top.zhimingw.tankgame3;

import java.util.Vector;

/**
 * @Author:zhimingw
 * @Version 1.0
 */
@SuppressWarnings("all")
public class EnemyTank extends Tank implements Runnable{
    //保存Shot
    Vector<Shot> shots = new Vector<>();
    boolean isLive = true;
    public EnemyTank(int x,int y){
        super(x,y);
    }

    @Override
    public void run() {
        while(true){
            if(isLive&&shots.size()<5){
                Shot s = null;
                switch (getDirect()){
                    case 0:
                        s = new Shot(getX()+20,getY(),0);
                        break;
                    case 1:
                        s = new Shot(getX()+60,getY()+20,1);
                        break;
                    case 2:
                        s = new Shot(getX()+20,getY()+60,2);
                        break;
                    case 3:
                        s = new Shot(getX(),getY()+20,3);
                        break;
                }
                shots.add(s);
                new Thread(s).start();
            }

            //根据坦克方向来继续移动
            switch (getDirect()){
                case 0:
                    for(int i=0;i<20;i++){
                        if(getY()>0){
                            moveUp();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1:
                    for(int i=0;i<20;i++){
                        if(getX()<940){
                            moveRight();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:
                    for(int i=0;i<20;i++){
                        if(getY()<690){
                            moveDown();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:
                    for(int i=0;i<20;i++){
                        if(getX()>0){
                            moveLeft();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }

            //然后随机的改变坦克的方向
            setDirect((int)(Math.random()*4));
            //什么时候终止线程
            if(!isLive){
                break;
            }
        }
    }
}
